<script setup lang="ts">
import { ref } from 'vue'

interface Article {
  id: number
  title: string
  summary: string
  publishDate: string
  views: number
  likes: number
  comments: number
  status: 'published' | 'draft'
}

const articles = ref<Article[]>([
  {
    id: 1,
    title: '如何使用Vue3的组合式API',
    summary: '本文介绍了Vue3组合式API的基本用法和最佳实践...',
    publishDate: '2024-02-07',
    views: 1234,
    likes: 56,
    comments: 23,
    status: 'published'
  },
  {
    id: 2,
    title: 'TypeScript入门指南',
    summary: '从零开始学习TypeScript，包含基础语法和进阶用法...',
    publishDate: '2024-02-05',
    views: 890,
    likes: 45,
    comments: 12,
    status: 'published'
  },
  {
    id: 3,
    title: '前端性能优化技巧',
    summary: '分享一些实用的前端性能优化方法和工具...',
    publishDate: '2024-02-01',
    views: 567,
    likes: 34,
    comments: 8,
    status: 'draft'
  }
])

const handleEdit = (article: Article) => {
  // 实现编辑功能
  console.log('编辑文章:', article)
}

const handleDelete = (article: Article) => {
  // 实现删除功能
  console.log('删除文章:', article)
}

const handlePublish = (article: Article) => {
  // 实现发布功能
  console.log('发布文章:', article)
}
</script>

<template>
  <div class="articles-container">
    <div class="header">
      <h2>我的文章</h2>
      <el-button type="primary" @click="$router.push('/home/blog/write')">
        <i class="fa fa-plus"></i>
        写文章
      </el-button>
    </div>

    <div class="article-list">
      <div v-for="article in articles" :key="article.id" class="article-card">
        <div class="article-content">
          <h3 class="article-title">{{ article.title }}</h3>
          <p class="article-summary">{{ article.summary }}</p>
          <div class="article-meta">
            <span class="meta-item">
              <i class="fa fa-calendar"></i>
              {{ article.publishDate }}
            </span>
            <span class="meta-item">
              <i class="fa fa-eye"></i>
              {{ article.views }}
            </span>
            <span class="meta-item">
              <i class="fa fa-heart"></i>
              {{ article.likes }}
            </span>
            <span class="meta-item">
              <i class="fa fa-comments"></i>
              {{ article.comments }}
            </span>
            <el-tag 
              :type="article.status === 'published' ? 'success' : 'info'"
              size="small"
            >
              {{ article.status === 'published' ? '已发布' : '草稿' }}
            </el-tag>
          </div>
        </div>
        <div class="article-actions">
          <el-button-group>
            <el-button type="primary" size="small" @click="handleEdit(article)">
              <i class="fa fa-edit"></i>
              编辑
            </el-button>
            <el-button 
              v-if="article.status === 'draft'"
              type="success" 
              size="small" 
              @click="handlePublish(article)"
            >
              <i class="fa fa-paper-plane"></i>
              发布
            </el-button>
            <el-button type="danger" size="small" @click="handleDelete(article)">
              <i class="fa fa-trash"></i>
              删除
            </el-button>
          </el-button-group>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.articles-container {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.header h2 {
  margin: 0;
  font-size: 24px;
  font-weight: 500;
}

.article-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.article-card {
  background-color: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.article-content {
  flex: 1;
  margin-right: 20px;
}

.article-title {
  margin: 0 0 8px 0;
  font-size: 18px;
  font-weight: 500;
  color: #333;
}

.article-summary {
  margin: 0 0 12px 0;
  color: #666;
  font-size: 14px;
  line-height: 1.5;
}

.article-meta {
  display: flex;
  align-items: center;
  gap: 16px;
  color: #999;
  font-size: 13px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

.meta-item i {
  font-size: 14px;
}

.article-actions {
  display: flex;
  gap: 8px;
}

@media (max-width: 768px) {
  .article-card {
    flex-direction: column;
    align-items: flex-start;
  }

  .article-content {
    margin-right: 0;
    margin-bottom: 16px;
  }

  .article-meta {
    flex-wrap: wrap;
    gap: 12px;
  }
}
</style> 